//
//  DestnationCountryDiscountSuperVC.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "DestnationCountryDiscountSuperVC.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "New_discountTableViewCell.h"
#import "CountryViewController.h"

static NSString *const discountReuse = @"discountReuse";


@interface DestnationCountryDiscountSuperVC ()

@property (nonatomic, strong) UITableView *myTabelView;

@end

@implementation DestnationCountryDiscountSuperVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame = CGRectMake(0, 0, WIDTH , HEIGHT);
    
    self.myTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT - 20) style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTabelView];
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    
    //注册
    UINib *nib = [UINib nibWithNibName:@"New_discountTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nib forCellReuseIdentifier:discountReuse];

    
    //返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 10, 40, 40);
    button.backgroundColor = [UIColor clearColor];
    [button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; //btn-fanhui64x64
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAtcion) forControlEvents:UIControlEventTouchUpInside];
    
//#warning  通知中心第一步: 注册一个通知
    //参数name:是消息通知的标识符, 只有发送者跟接收者的name参数一样, 接收者才能接收到发送者的消息.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coutyID:) name:@"contyID" object:nil];
    
//    NSLog(@"")
    

    
    // Do any additional setup after loading the view.
}

- (void)setCountyID:(NSString *)countyID
{
    NSLog(@"Country id is %@", countyID);
    if ([_countyID isEqualToString:countyID]) {
        return;
    }
    
    _countyID = countyID;
    [self getData];
}

-(void)coutyID:(NSNotification *)noti
{
    self.countyID = [noti object];
    NSLog(@"countyID=====4==========%@",self.countyID);
    [self.myTabelView reloadData];
}


//data
-(void)getData{
    self.myArray = [NSMutableArray array];
    
    DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1  getDestinationCountyDiscountSuper:self.countyID succeedHandler:^(id result) {
        self.myArray = [NSMutableArray arrayWithArray:result];
        [self.myTabelView reloadData];
    } failedHandler:^(NSError *error) {
    }];
}

- (void)buttonAtcion
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    New_discountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:discountReuse];
    DestnationCountryDiscountSuperModel *model = [self.myArray objectAtIndex:indexPath.row];
    cell.mySuperModel = model;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
